#pragma once
#ifndef OCTATREE_H
#define OCTATREE_H

#include "tricell.h"
#include <cstdio>
#include <vector>

#define MAXOBJ 100
#ifndef INF
	#define INF 1.0e+20
#endif

#ifndef SMALL
	#define SMALL 1.0e-12
#endif

class OctaTree
{
public:
		OctaTree(double bnd[][2], int size);
		OctaTree(OctaTree &octree);
		~OctaTree(void);
		void GetBnd(double bnd[][2], int size);
		int GetCount(void);
		TriCell* GetObj(int no_obj);
		OctaTree* GetChildNode(int no_child);
		void CreateChildNode(void);
		int Insert(TriCell *objcell);
		void SetInserted(bool ins);
		bool IsInserted(void);
		int RayIntersect(double ray_pos[3], double ray_dir[3], double& ray_len, int rank);
		friend void proc_subtree(double tx0,double ty0,double tz0,double tx1,double ty1,double tz1, OctaTree *n, double ray_pos[3], double ray_dir[3], int &tri_no, unsigned char flagbit, int rank);
		friend void proc_subtreeA(double tx0,double ty0,double tz0,double tx1,double ty1,double tz1, OctaTree *n, unsigned char flagbit, int rank);
private:
	//行表示X,Y,Z，列表示左边界和右边界;
	double bnd[3][2];
	 //节点对象数目
	int count;
	//是否已经插入子节点
	bool bHasChild;
	//节点对象指针数组
	TriCell* obj[MAXOBJ];
	//子节点指针数组
	OctaTree* child[8];
	//子节点顺序
	//1 -X-Y-Z
	//2 +X-Y-Z
	//3 +X+Y-Z
	//4 -X+Y-Z
	//5 -X-Y+Z
	//6 +X-Y+Z
	//7 +X+Y+Z
	//8 -X+Y+Z
	static vector<TriCell*> Intersected_TriCell;
};
#endif // !OCTATREE_H